package com.vq3.digitalizationsoftwareintegrates.Servlet.User;




import com.vq3.digitalizationsoftwareintegrates.Entity.Enum.Group_Type;
import com.vq3.digitalizationsoftwareintegrates.Entity.Info.ResultInfo;
import com.vq3.digitalizationsoftwareintegrates.Entity.Spot_Check.MongoDBEntity.MongoDB_S_C_SysAuthAuditMessageEntity;
import com.vq3.digitalizationsoftwareintegrates.Entity.User.UserEntity;
import com.vq3.digitalizationsoftwareintegrates.Service.User.UserLoginService;
import com.vq3.digitalizationsoftwareintegrates.Utils.ImageUtil.CalculationIsType;
import com.vq3.digitalizationsoftwareintegrates.Utils.PackagedDataUtil.Impl.PackagedDataImpl;
import com.vq3.digitalizationsoftwareintegrates.Utils.PackagedDataUtil.PackagedData;
import com.vq3.digitalizationsoftwareintegrates.Utils.PermissionControl.RegularValidation;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@RestController
@RequestMapping("user")
public class UserLogin {
    final UserLoginService userLoginService;

    final AuthenticationManager authenticationManager;

    public UserLogin(UserLoginService userLoginService,AuthenticationManager authenticationManager) {
        this.userLoginService = userLoginService;

        this.authenticationManager = authenticationManager;
    }
    @RequestMapping("/getUserParam")
    @ResponseBody
    public void getUserParam(HttpServletRequest httpServletRequest,HttpServletResponse httpServletResponse) throws IOException {
        UserEntity userEntity = (UserEntity)httpServletRequest.getSession().getAttribute("USER");
        ResultInfo resultInfo = new ResultInfo();
        PackagedData packagedData = new PackagedDataImpl();
        if (userEntity == null) {
            resultInfo.setFlag(false);
            resultInfo.setErrorMsg("请登录！");
        }else {
            resultInfo.setFlag(true);
            resultInfo.setData(userEntity);
        }
        packagedData.writeValue(resultInfo,httpServletResponse);
    }

    @RequestMapping("/loginFail")
    public void loginFail(HttpServletRequest httpServletRequest,HttpServletResponse httpServletResponse) throws IOException {
        Object resultInfo = httpServletRequest.getAttribute("resultInfo");
        System.out.println(resultInfo);
        PackagedData packagedData = new PackagedDataImpl();
        packagedData.writeValue(resultInfo,httpServletResponse);
    }
    @RequestMapping("SysAuthentication")
    public void SysAuthentication(@RequestParam String fullName, @RequestParam Group_Type group , HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws IOException {
        MongoDB_S_C_SysAuthAuditMessageEntity messageEntity = new MongoDB_S_C_SysAuthAuditMessageEntity();
        PackagedData packagedData = new PackagedDataImpl();
        ResultInfo resultInfo = new ResultInfo();
        boolean flag;
        messageEntity.setExistUserEntity((UserEntity) httpServletRequest.getSession().getAttribute("USER"));
        if (RegularValidation.SysAuthenticationVerify(fullName) && null!= group) {
            UserEntity userEntity = new UserEntity();
            userEntity.setFullname(fullName);
            userEntity.setGroupId(CalculationIsType.calculationIsGroupTypeToMySql(group));
            messageEntity.setToUserEntity(userEntity);
            messageEntity.setAuditGroup(group);
            flag = userLoginService.SysAuthentication(messageEntity);
        }else {
            flag = false;
        }
        resultInfo.setFlag(flag);
        packagedData.writeValue(resultInfo,httpServletResponse);
    }

    @RequestMapping("uidFinUserName")
    public void uidFinUserName(@RequestParam Integer uid,HttpServletResponse httpServletResponse,HttpServletRequest httpServletRequest) throws IOException {
        UserEntity userEntity = userLoginService.uidFindUser(uid);
        ResultInfo resultInfo = new ResultInfo();
        PackagedData packagedData = new PackagedDataImpl();
        if (userEntity != null) {
            UserEntity userEntity1 = new UserEntity(uid, userEntity.getUname(), null, userEntity.getFullname(), null, null, null, null, null, null);
            resultInfo.setFlag(true);
            resultInfo.setData(userEntity1);
        }
        packagedData.writeValue(resultInfo,httpServletResponse);



    }

    @RequestMapping("VerifyLogin")
    public void VerifyLogin(HttpServletRequest httpServletRequest) throws ServletException {
        httpServletRequest.logout();
    }
}
